import {makeAutoObservable, runInAction} from 'mobx';

export interface IPage {
    id: string,
    parent?: string,
    label: string,
    icon: string,
    schema: any,
    children: IPage[],
}

export interface IPageStore {

    getList(): IPage[] | [];

    getTree(): IPage | undefined;

    getById(id: string): IPage | undefined;

    add(page: IPage): void;

    update(page: IPage): void;

    delete(pageId: string): Promise<boolean>;
}

export class PageStore implements IPageStore {

    constructor() {
        makeAutoObservable(this);
    }

    getList(): IPage[] | [] {
        let pages: IPage[] = [];
        if (typeof window !== 'undefined' && window.localStorage) {
            const pagesData = window.localStorage.getItem('pages');
            if (pagesData) {
                pages = JSON.parse(pagesData);
            } else {
                let root: IPage = {
                    id: 'root',
                    label: 'root',
                    icon: '',
                    schema: {
                        type: 'page',
                        title: '默认页面',
                        body: '默认页面'
                    },
                    children: [],
                };
                pages.push(root);
                window.localStorage.setItem('pages', JSON.stringify(pages));
            }
        }
        return pages;
    }

    getTree(): IPage | undefined {
        let pages: IPage[] = this.getList()
        let pageTree: IPage = pages[0]

        const addInTree = (subTree: IPage, page: IPage): boolean => {
            if (subTree.id == page.parent) {
                subTree.children.push(page)
                return true
            } else {
                let done = false
                subTree.children.find(item => {
                    done = addInTree(item, page)
                    return done
                });
                return done
            }
        }
        pages.forEach(item => {
            if (item.id != 'root') {
                addInTree(pageTree, item)
            }
        })
        return pageTree
    }

    getById(id: string): IPage | undefined {
        let pages: IPage[] = this.getList()
        return pages.find(item => item.id == id)
    }

    add(page: IPage): void {
        let pages: IPage[] = this.getList()
        pages.push(page)
        window.localStorage.setItem('pages', JSON.stringify(pages));
    }

    delete(pageId: string): Promise<boolean> {
        if (pageId == 'root') {
            return Promise.reject(new Error("不能删除根页面"))
        }
        let pages: IPage[] = this.getList()
        pages = pages.filter(item => item.id != pageId)
        window.localStorage.setItem('pages', JSON.stringify(pages));
        return Promise.resolve(true)
    }

    update(page: IPage): void {
        let pages: IPage[] = this.getList()
        let pageOld = pages.find(item => item.id == page.id)
        if (pageOld) {
            pageOld.parent = page.parent;
            pageOld.label = page.label;
            pageOld.icon = page.icon;
            pageOld.schema = page.schema;
            pageOld.children = page.children;
        }

        window.localStorage.setItem('pages', JSON.stringify(pages));
    }
}